package com.xoa.service.hr.learningExperience;

import com.xoa.dao.common.HrCodeMapper;
import com.xoa.dao.hr.learningExperience.LearningExperienceMapper;
import com.xoa.dao.users.UsersMapper;
import com.xoa.model.common.HrCode;
import com.xoa.model.common.HrCodeExample;
import com.xoa.model.enclosure.Attachment;
import com.xoa.model.hr.learningExperience.LearningExperience;
import com.xoa.model.hr.learningExperience.LearningExperienceExample;
import com.xoa.model.hr.learningExperience.LearningExperienceResult;
import com.xoa.model.hr.learningExperience.LearningExperienceWithBLOBs;
import com.xoa.model.users.Users;
import com.xoa.util.ExcelUtil;
import com.xoa.util.GetAttachmentListUtil;
import com.xoa.util.ToJson;
import com.xoa.util.common.StringUtils;
import com.xoa.util.common.log.FileUtils;
import com.xoa.util.common.wrapper.BaseWrapper;
import com.xoa.util.page.PageParams;
import org.apache.poi.hssf.usermodel.*;
import org.apache.poi.hssf.util.HSSFColor;
import org.apache.poi.ss.usermodel.FillPatternType;
import org.apache.poi.ss.usermodel.HorizontalAlignment;
import org.apache.poi.ss.usermodel.IndexedColors;
import org.apache.poi.ss.usermodel.VerticalAlignment;
import org.apache.poi.ss.util.CellRangeAddress;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.*;

@Service
public class LearningExperienceService {
    @Autowired
    LearningExperienceMapper learningExperienceMapper;
    @Autowired
    UsersMapper usersMapper;
    @Autowired
    HrCodeMapper hrCodeMapper;

    private static final String HR_SCHOOL="STAFF_HIGHEST_SCHOOL";
    private static final String HR_DEGREE="EMPLOYEE_HIGHEST_DEGREE";

    /**
    * @author 程叶同
    * @date 2018/7/2 12:35
    * @desc 学习经历新建
    */
    public BaseWrapper newLearningExperience(LearningExperienceWithBLOBs learningExperienceWithBLOBs){
        BaseWrapper baseWrapper=new BaseWrapper();
        String substring="";
        if (learningExperienceWithBLOBs != null) {
            if (learningExperienceWithBLOBs.getUserId() != null) {
                String userId = learningExperienceWithBLOBs.getUserId();
                if (userId.contains(",")) {
                    substring = userId.substring(0, userId.length() - 1);
                    learningExperienceWithBLOBs.setUserId(substring);
                }
                learningExperienceWithBLOBs.setLastmodifyTime(new Date());
                LearningExperienceExample learningExperienceExample=new LearningExperienceExample();
                LearningExperienceExample.Criteria criteria = learningExperienceExample.createCriteria();
                criteria.andUserIdEqualTo(substring);
                List<LearningExperienceWithBLOBs> learningExperienceWithBLOBs1 = learningExperienceMapper.selectByExampleWithBLOBs(learningExperienceExample);
                int i=0;
                if (learningExperienceWithBLOBs1 != null) {
                    if (learningExperienceWithBLOBs1.size() > 0) {
                        learningExperienceWithBLOBs.setRegisterTime(learningExperienceWithBLOBs1.get(0).getRegisterTime());
                        learningExperienceWithBLOBs.setId(learningExperienceWithBLOBs1.get(0).getId());
                        i=learningExperienceMapper.updateByPrimaryKeyWithBLOBs(learningExperienceWithBLOBs);
                    }else{
                        learningExperienceWithBLOBs.setRegisterTime(new Date());
                        i= learningExperienceMapper.insertSelective(learningExperienceWithBLOBs);
                    }
                }else{
                    learningExperienceWithBLOBs.setRegisterTime(new Date());
                    i=learningExperienceMapper.insertSelective(learningExperienceWithBLOBs);
                }
                if (i > 0) {
                    baseWrapper.setFlag(true);
                    baseWrapper.setStatus(true);
                    baseWrapper.setMsg("新建成功");
                }
            }
        }
        return baseWrapper;
    }
    /**
    * @author 程叶同
    * @date 2018/7/2 12:37
    * @desc  单个信息查询
    */
    public BaseWrapper getLerningExperienceById(HttpServletRequest request,Integer id){
        BaseWrapper baseWrapper=new BaseWrapper();
        String  sqlType = "xoa" + (String) request.getSession().getAttribute(
                "loginDateSouse");
        if (id == 0 || id == null) {
            baseWrapper.setMsg("参数获取错误");
            return  baseWrapper;
        }
        LearningExperienceWithBLOBs learningExperienceWithBLOBs = learningExperienceMapper.selectByPrimaryKey(id);
        if (learningExperienceWithBLOBs != null) { //获取附件
            LearningExperienceResult learningExperienceResult=new LearningExperienceResult();
            BeanUtils.copyProperties(learningExperienceWithBLOBs,learningExperienceResult);
            String usernameByUserId = usersMapper.getUsernameByUserId(learningExperienceResult.getUserId());
            List<Attachment> attachments = GetAttachmentListUtil.returnAttachment(learningExperienceResult.getAttachmentName(), learningExperienceResult.getAttachmentId(), sqlType, GetAttachmentListUtil.MODULE_HR);
            learningExperienceResult.setAttachmentList(attachments);
            if (usernameByUserId != null) {
                learningExperienceResult.setUserName(usernameByUserId);
            }else{
                learningExperienceResult.setUserName("");
            }
            baseWrapper.setMsg("查询成功");
            baseWrapper.setFlag(true);
            baseWrapper.setStatus(true);
            baseWrapper.setData(learningExperienceResult);
        }
        return  baseWrapper;
    }
    /**
    * @author 程叶同
    * @date 2018/7/2 12:47
    * @desc 修改学习经历
    */
    public BaseWrapper updateLearningExperience(LearningExperienceWithBLOBs experienceWithBLOBs){
        BaseWrapper baseWrapper=new BaseWrapper();
        if (experienceWithBLOBs != null) {
            if (experienceWithBLOBs.getId()==null||experienceWithBLOBs.getId()==0) {
                baseWrapper.setMsg("参数错误");
                return baseWrapper;
            }else{
                experienceWithBLOBs.setLastmodifyTime(new Date());
                LearningExperienceWithBLOBs learningExperienceWithBLOBs = learningExperienceMapper.selectByPrimaryKey(experienceWithBLOBs.getId());
                if (learningExperienceWithBLOBs != null) {
                    experienceWithBLOBs.setRegisterTime(learningExperienceWithBLOBs.getRegisterTime());
                }
                int i = learningExperienceMapper.updateByPrimaryKeyWithBLOBs(experienceWithBLOBs);
                if (i > 0) {//导入是否成功
                    baseWrapper.setFlag(true);
                    baseWrapper.setMsg("修改成功");
                    baseWrapper.setStatus(true);
                }
            }
        }
        return  baseWrapper;
    }
    /**
    * @author 程叶同
    * @date 2018/7/2 13:02
    * @desc  学习经历删除
    */
    public BaseWrapper deleteLearningExperienceByIds(Integer[] ids){
        BaseWrapper baseWrapper=new BaseWrapper();
        if (ids.length == 1) {
            int i = learningExperienceMapper.deleteByPrimaryKey(ids[0]);
            if (i > 0) {
                baseWrapper.setStatus(true);
                baseWrapper.setFlag(true);
                baseWrapper.setMsg("删除成功");
            }
        }
        if (ids.length > 1) {
            int i = learningExperienceMapper.deleteLearningExperienceByids(ids);
            if (i > 0) {
                baseWrapper.setStatus(true);
                baseWrapper.setFlag(true);
                baseWrapper.setMsg("删除成功");
            }
        }
        return  baseWrapper;
    }
    /**
    * @author 程叶同
    * @date 2018/7/2 13:46
    * @desc 列表显示
    */
    public  BaseWrapper getLearningExperienceList(HttpServletRequest request,Integer page,Integer pageSize,boolean userFlag,LearningExperienceWithBLOBs learningExperienceWithBLOBs){
        BaseWrapper baseWrapper=new BaseWrapper();
        String  sqlType = "xoa" + (String) request.getSession().getAttribute(
                "loginDateSouse");
        List<LearningExperienceResult> learningExperienceList=new ArrayList<>();
        PageParams pageParams=new PageParams();
        pageParams.setUseFlag(userFlag);
        pageParams.setPage(page);
        pageParams.setPageSize(pageSize);
        String userId=learningExperienceWithBLOBs.getUserId();
        String[] userIds=null;
        if (userId != null&&userId!="") {
            userIds = userId.split(",");
        }
        Map<String,Object> map=new HashMap<>();
        map.put("userId",userIds);
        map.put("profession",learningExperienceWithBLOBs.getProfession());
        map.put("academic",learningExperienceWithBLOBs.getAcademic());
        map.put("university",learningExperienceWithBLOBs.getUniversity());
        map.put("certificate",learningExperienceWithBLOBs.getCertificate());
        map.put("pageParams",pageParams);
        Map<String,Object> maps=new HashMap<>();
        maps.put("userId",userIds);
        maps.put("profession",learningExperienceWithBLOBs.getProfession());
        maps.put("academic",learningExperienceWithBLOBs.getAcademic());
        maps.put("university",learningExperienceWithBLOBs.getUniversity());
        maps.put("certificate",learningExperienceWithBLOBs.getCertificate());
         learningExperienceList = learningExperienceMapper.getLearningExperienceList(map);
        List<LearningExperienceResult> countlist = learningExperienceMapper.getLearningExperienceList(maps);
        if (learningExperienceList != null) {
            if (learningExperienceList.size() > 0) {
                for (LearningExperienceResult le : learningExperienceList) {
                    List<Attachment> attachments = GetAttachmentListUtil.returnAttachment(le.getAttachmentName(), le.getAttachmentId(), sqlType, GetAttachmentListUtil.MODULE_HR);
                    le.setAttachmentList(attachments);
                }
            }
            baseWrapper.setMsg("查询成功");
            baseWrapper.setFlag(true);
            baseWrapper.setStatus(true);
            baseWrapper.setData(learningExperienceList);
            baseWrapper.setTotleNum(countlist.size());
        }
        return  baseWrapper;
    }
/**
* @author 程叶同
* @date 2018/7/2 14:19
* @desc  导出
*/
    public void exportLearningExperience(HttpServletRequest request, HttpServletResponse response,LearningExperienceWithBLOBs learningExperienceWithBLOBs){
        SimpleDateFormat simpleDateFormat=new SimpleDateFormat("YYYY-MM-dd HH:mm:ss");
        try {
            Map<String, Object> map = new HashMap<>();
            map.put("userId",learningExperienceWithBLOBs.getUserId());
            map.put("profession",learningExperienceWithBLOBs.getProfession());
            map.put("academic",learningExperienceWithBLOBs.getAcademic());
            map.put("university",learningExperienceWithBLOBs.getUniversity());
            map.put("certificate",learningExperienceWithBLOBs.getCertificate());
            List<LearningExperienceResult> learningExperienceList = learningExperienceMapper.getLearningExperienceList(map);
           // HSSFWorkbook workbook1 = ExcelUtil.makeExcelHead("学习经历信息导出", 9);
            HSSFWorkbook workbooks = new HSSFWorkbook();
            //HSSFCellStyle styleTitle = createStyle(workbook, (short)16);
            HSSFCellStyle style = workbooks.createCellStyle();
            style.setAlignment(HorizontalAlignment.CENTER);
            style.setVerticalAlignment(VerticalAlignment.CENTER);
            // 创建一个字体样式
            HSSFFont font = workbooks.createFont();
            font.setFontHeightInPoints((short)16);
            font.setBold(true);
            style.setFont(font);
            HSSFSheet sheet = workbooks.createSheet("学习经历信息导出");
            sheet.setDefaultColumnWidth(25);
            sheet.setDefaultRowHeightInPoints(25);
            CellRangeAddress cellRangeAddress = new CellRangeAddress(0, 0, 0, 13);
            sheet.addMergedRegion(cellRangeAddress);
            HSSFRow rowTitle = sheet.createRow(0);
            HSSFCell cellTitle = rowTitle.createCell(0);
            // 为标题设置背景颜色
            style.setFillPattern(FillPatternType.SOLID_FOREGROUND);
            style.setFillForegroundColor(IndexedColors.GREY_25_PERCENT.index);
            cellTitle.setCellValue("学习经历信息导出");
            cellTitle.setCellStyle(style);
            String[] secondTitles = {"序号", "单位员工", "所学专业", "开始时间", "结束时间", "所获学历", "所获学位", "曾任班干", "证明人", "所在院校",
                    "院校所在地", "获奖情况", "所获证书", "备注"
            };
            HSSFWorkbook workbook2 = ExcelUtil.makeSecondHead(workbooks, secondTitles);
            int i = 1;
            for (LearningExperienceResult learningExperienceResult : learningExperienceList) {
                Date startTime = learningExperienceResult.getStartTime();
                if (startTime != null) {
                    String startTimeStr = simpleDateFormat.format(startTime);
                    learningExperienceResult.setStartTimeStr(startTimeStr);
                }else{
                    learningExperienceResult.setStartTimeStr("");
                }
                Date endTime = learningExperienceResult.getEndTime();
                if (endTime != null) {
                    String endTimeStr = simpleDateFormat.format(endTime);
                    learningExperienceResult.setEndTimeStr(endTimeStr);
                }else{
                    learningExperienceResult.setEndTimeStr("");
                }
                learningExperienceResult.setId(i);
                switch(learningExperienceResult.getAcademic()){
                case"1":
                    learningExperienceResult.setAcademic("小学");
                    break;
                case "2":
                    learningExperienceResult.setAcademic("初中");
                    break;
                    case "3":
                        learningExperienceResult.setAcademic("高中");
                        break;
                    case "4":
                        learningExperienceResult.setAcademic("中专");
                        break;
                    case "5":
                        learningExperienceResult.setAcademic("大专");
                        break;
                    case "6":
                        learningExperienceResult.setAcademic("本科");
                        break;
                    case "7":
                        learningExperienceResult.setAcademic("研究生");
                        break;
                    case "8":
                        learningExperienceResult.setAcademic("博士");
                        break;
                    case "9":
                        learningExperienceResult.setAcademic("博士后");
                        break;
                    case "10":
                        learningExperienceResult.setAcademic("技校");
                        break;

                default:
                    learningExperienceResult.setAcademic("其他");
                    break;
            }
            switch(learningExperienceResult.getDegree()){
                    case"1":
                        learningExperienceResult.setDegree("博士后");
                        break;
                    case"2":
                        learningExperienceResult.setDegree("博士");
                        break;
                    case"3":
                        learningExperienceResult.setDegree("MBA");
                        break;
                    case"4":
                        learningExperienceResult.setDegree("硕士");
                        break;
                    case"5":
                        learningExperienceResult.setDegree("双学士");
                        break;
                    case"6":
                        learningExperienceResult.setDegree("学士");
                        break;
                    default:
                        learningExperienceResult.setAcademic("无");
                        break;
                }



                i++;
            }
            String[] beanProperty = {"id", "userName", "profession", "startTimeStr", "endTimeStr", "academic", "degree", "classcadre", "certifier", "university", "universityLocation", "honor", "certificate", "remark"};
            HSSFWorkbook workbook = ExcelUtil.exportExcelData(workbook2, learningExperienceList, beanProperty);
            ServletOutputStream out = response.getOutputStream();
            String filename = "学习经历信息导出.xls"; //考虑多语言
            filename = FileUtils.encodeDownloadFilename(filename,
                    request.getHeader("user-agent"));
            response.setContentType("application/vnd.ms-excel");
            response.setHeader("content-disposition",
                    "attachment;filename=" + filename);
            workbook.write(out);
            out.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }
    /**
     * @author 程叶同
     * @date 2018/7/2 14:39
     * @desc  学习经历导入
     */
    public BaseWrapper importLearningExperience(MultipartFile file) throws IOException {
        BaseWrapper baseWrapper = new BaseWrapper();
        //读取当前配置
        ResourceBundle rb = ResourceBundle.getBundle("upload");
        String osName = System.getProperty("os.name");
        StringBuffer path = new StringBuffer();
             StringBuffer buffer=new StringBuffer();
        InputStream is=null;
        try {
            if(osName.toLowerCase().startsWith("win")){
                //sb=sb.append(rb.getString("upload.win"));
                //判断路径是否是相对路径，如果是的话，加上运行的路径
                String uploadPath = rb.getString("upload.win");
                if(uploadPath.indexOf(":")==-1){
                    //获取运行时的路径
                    String basePath = this.getClass().getClassLoader().getResource(".").getPath()+ File.pathSeparator;
                    //获取截取后的路径
                    String str2 = "";
                    if(basePath.indexOf("/xoa")!=-1){
                        //获取字符串的长度
                        int length = basePath.length();
                        //返回指定字符在此字符串中第一次出现处的索引
                        int index = basePath.indexOf("/xoa");
                        //从指定位置开始到指定位置结束截取字符串
                        str2=basePath.substring(0,index);
                    }
                    path = path.append(str2 + "/xoa");
                }
                path.append(uploadPath);
                buffer=buffer.append(rb.getString("upload.win"));
            }else{
                path=path.append(rb.getString("upload.linux"));
                buffer=buffer.append(rb.getString("upload.linux"));
            }
            int success=0;
            // 判断是否为空文件
            if (file.isEmpty()) {
                /*json.setMsg("请上传文件！");
                json.setFlag(1);
                return json;*/
            } else {
                File file1=new File(path.toString());
                if (!file1.exists()) {
                    file1.mkdirs();
                }
                String fileName = file.getOriginalFilename();
                if (fileName.endsWith(".xls") || fileName.endsWith(".xlsx")) {
                    String uuid = UUID.randomUUID().toString().replaceAll("-", "");
                    int pos = fileName.indexOf(".");
                    String extName = fileName.substring(pos);
                    String newFileName = uuid + extName;
                    File dest = new File(path.toString(),newFileName);
                    file.transferTo(dest);
                    // 将文件上传成功后进行读取文件
                    // 进行读取的路径
                    String readPath = path.append(System.getProperty("file.separator")).append(newFileName).toString();

                    is = new FileInputStream(readPath);
                            HSSFWorkbook hssfWorkbook = new HSSFWorkbook(is);
                            LearningExperienceWithBLOBs learningExperienceWithBLOBs = null;
                            List<LearningExperienceWithBLOBs> list = new ArrayList<LearningExperienceWithBLOBs>();
                            // 循环工作表Sheet
                            for (int numSheet = 0; numSheet < hssfWorkbook.getNumberOfSheets(); numSheet++) {
                                     HSSFSheet hssfSheet = hssfWorkbook.getSheetAt(numSheet);
                                   if (hssfSheet == null) {
                                             continue;
                                         }
                                     // 循环行Row
                                     for (int rowNum = 3; rowNum <= hssfSheet.getLastRowNum(); rowNum++) {
                                            HSSFRow hssfRow = hssfSheet.getRow(rowNum);
                                            if (hssfRow != null) {
                                                learningExperienceWithBLOBs = new LearningExperienceWithBLOBs();
                                                    HSSFCell userId = hssfRow.getCell(0);
                                                   HSSFCell profession = hssfRow.getCell(1);
                                                     HSSFCell startTime = hssfRow.getCell(2);
                                                     HSSFCell endTime = hssfRow.getCell(3);
                                                     HSSFCell academic = hssfRow.getCell(4);
                                                     HSSFCell degree = hssfRow.getCell(5);
                                                     HSSFCell classcadre = hssfRow.getCell(6);
                                                     HSSFCell certificer = hssfRow.getCell(7);
                                                     HSSFCell university = hssfRow.getCell(8);
                                                     HSSFCell universityLocation = hssfRow.getCell(9);
                                                     HSSFCell honor = hssfRow.getCell(10);
                                                     HSSFCell certificate = hssfRow.getCell(11);
                                                     HSSFCell remark = hssfRow.getCell(12);
                                                Users userByName = usersMapper.getUserByName(userId.getStringCellValue());
                                                if (userByName==null) {
                                                    baseWrapper.setMsg("没有该用户");
                                                    return baseWrapper;
                                                }
                                                if (userByName != null) {
                                                    learningExperienceWithBLOBs.setUserId(userByName.getUserId());
                                                }else{
                                                    learningExperienceWithBLOBs.setUserId("");
                                                }
                                                //learningExperienceWithBLOBs.setUserId(userId.getStringCellValue());
                                                learningExperienceWithBLOBs.setProfession(profession.getStringCellValue());
                                                try {
                                                    Date st = new SimpleDateFormat("yyyy-MM-dd").parse(startTime.getStringCellValue());
                                                    Date et = new SimpleDateFormat("yyyy-MM-dd").parse(endTime.getStringCellValue());
                                                    learningExperienceWithBLOBs.setEndTime(et);
                                                    learningExperienceWithBLOBs.setStartTime(st);
                                                } catch (ParseException e) {
                                                    e.printStackTrace();
                                                }
                                                learningExperienceWithBLOBs.setLastmodifyTime(new Date());


                                                String s="";
                                                if (degree!=null) {
                                                    String degreeStr=degree.getStringCellValue();
                                                    switch (degreeStr) {
                                                        case "博士后": s="1";break;
                                                        case "博士": s="2";break;
                                                        case "MBA": s="3";break;
                                                        case "硕士": s="4";break;
                                                        case "双学士": s="5";break;
                                                        case "学士": s="6";break;
                                                    }
                                                }else{
                                                    s="";
                                                }
                                                String de="";
                                                if (academic != null) {
                                                    String acadeStr = academic.getStringCellValue();
                                                    switch (acadeStr) {
                                                        case "小学": de="1";break;
                                                        case "初中": de="2";break;
                                                        case "高中": de="3";break;
                                                        case "中专": de="4";break;
                                                        case "大专": de="5";break;
                                                        case "本科": de="6";break;
                                                        case "研究生": de="7";break;
                                                        case "博士": de="8";break;
                                                        case "博士后": de="9";break;
                                                        case "技校": de="10";break;
                                                    }
                                                }else{
                                                    de="";
                                                }
                                                learningExperienceWithBLOBs.setAcademic(de);
                                                learningExperienceWithBLOBs.setDegree(s);
                                                if (certificer != null) {
                                                    learningExperienceWithBLOBs.setCertifier(certificer.getStringCellValue());
                                                }
                                                if (classcadre!= null) {
                                                    learningExperienceWithBLOBs.setClasscadre(classcadre.getStringCellValue());
                                                }
                                                if (university != null) {
                                                    learningExperienceWithBLOBs.setUniversity(university.getStringCellValue());
                                                }
                                                if (universityLocation != null) {
                                                    learningExperienceWithBLOBs.setUniversityLocation(universityLocation.getStringCellValue());
                                                }
                                                if (honor!= null) {
                                                    learningExperienceWithBLOBs.setHonor(honor.getStringCellValue());
                                                }
                                                if (certificate!=null) {
                                                    learningExperienceWithBLOBs.setCertificate(certificate.getStringCellValue());
                                                }
                                                if (remark != null) {
                                                    learningExperienceWithBLOBs.setRemark(remark.getStringCellValue());
                                                }
                                                LearningExperienceExample learningExperienceExample=new LearningExperienceExample();
                                                LearningExperienceExample.Criteria criteria = learningExperienceExample.createCriteria();
                                                criteria.andUserIdEqualTo(userByName.getUserId());
                                                List<LearningExperienceWithBLOBs> learningExperienceWithBLOBs1 = learningExperienceMapper.selectByExampleWithBLOBs(learningExperienceExample);
                                                int i=0;
                                                if (learningExperienceWithBLOBs1 != null) {
                                                    if (learningExperienceWithBLOBs1.size() > 0) {
                                                        learningExperienceWithBLOBs.setId(learningExperienceWithBLOBs1.get(0).getId());
                                                        learningExperienceWithBLOBs.setRegisterTime(learningExperienceWithBLOBs1.get(0).getRegisterTime());
                                                        i=learningExperienceMapper.updateByPrimaryKeyWithBLOBs(learningExperienceWithBLOBs);
                                                    }else{
                                                        learningExperienceWithBLOBs.setRegisterTime(new Date());
                                                       i= learningExperienceMapper.insertSelective(learningExperienceWithBLOBs);
                                                    }
                                                }else{
                                                    learningExperienceWithBLOBs.setRegisterTime(new Date());
                                                    i=learningExperienceMapper.insertSelective(learningExperienceWithBLOBs);
                                                }
                                                if (i > 0) {
                                                    baseWrapper.setFlag(true);
                                                    baseWrapper.setStatus(true);
                                                    baseWrapper.setMsg("导入成功");
                                                }
                                                }
                                         }
                                 }
                                 dest.delete();
                                 //导入结束 删除文件

                }
            }
        } catch (IOException e) {
            e.printStackTrace();
        } catch (IllegalStateException e) {
            e.printStackTrace();
        } finally {
            if(is!=null){
                is.close();
            }
        }
        return baseWrapper;
    }
    /**
    * @author 程叶同
    * @date 2018/7/23 16:33
    * @desc  人力资源系统代码表
    */
    public BaseWrapper getHrCodes(String type){
        BaseWrapper baseWrapper=new BaseWrapper();
        List<HrCode> hrCodes=new ArrayList<>();
        HrCodeExample hrCodeExample=new HrCodeExample();
        HrCodeExample.Criteria criteria = hrCodeExample.createCriteria();
        hrCodeExample.setOrderByClause("cast(CODE_NO as SIGNED INTEGER) ASC");
        if ("edu".equals(type)) { //获取学历
            criteria.andParentNoEqualTo(HR_SCHOOL);
             hrCodes = hrCodeMapper.selectByExampleWithBLOBs(hrCodeExample);
        } else if ("degree".equals(type)) {//获取学位
            criteria.andParentNoEqualTo(HR_DEGREE);
            hrCodes=hrCodeMapper.selectByExampleWithBLOBs(hrCodeExample);
        }
        baseWrapper.setMsg("ok");
        baseWrapper.setFlag(true);
        baseWrapper.setData(hrCodes);
        baseWrapper.setStatus(true);
        return  baseWrapper;
    }

    /**
     * 创建者：戚中校
     * 方法作用：根据条件查询数量
     * @param
     * @param deptId(传入的多个部门的编号)
     * @return 返回的json字符串中  nums是返回的结果的数量
     *
     */

    public ToJson<LearningExperience> selectNumByCon(LearningExperience record, Integer[] deptId) {
        ToJson toJson = new ToJson();
        try {
            Map<String, Object> map = new HashMap<String, Object>();
            if (deptId != null && deptId.length > 0){
                map.put("deptId",deptId);
            }
            map.put("record",record);
            List<LearningExperience> list = learningExperienceMapper.selectNumByCon(map);

            for(LearningExperience learningExperience:list){
                if(!StringUtils.checkNull(learningExperience.getDegree())) {
                    if (learningExperience.getDegree().equals("1")) {
                        learningExperience.setDegreeName("小学");
                    } else if (learningExperience.getDegree().equals("2")) {
                        learningExperience.setDegreeName("初中");
                    } else if (learningExperience.getDegree().equals("3")) {
                        learningExperience.setDegreeName("高中");
                    } else if (learningExperience.getDegree().equals("4")) {
                        learningExperience.setDegreeName("中专");
                    } else if (learningExperience.getDegree().equals("5")) {
                        learningExperience.setDegreeName("大专");
                    } else if (learningExperience.getDegree().equals("6")) {
                        learningExperience.setDegreeName("本科");
                    } else if (learningExperience.getDegree().equals("7")) {
                        learningExperience.setDegreeName("研究生");
                    } else if (learningExperience.getDegree().equals("8")) {
                        learningExperience.setDegreeName("博士");
                    } else if (learningExperience.getDegree().equals("9")) {
                        learningExperience.setDegreeName("博士后");
                    } else if (learningExperience.getDegree().equals("11")) {
                        learningExperience.setDegreeName("技校");
                    } else {
                        learningExperience.setDegreeName("其它");
                    }
                }
            }
            if(list.size() >= 0){
                toJson.setObj(list);
                toJson.setMsg("ok");
                toJson.setFlag(0);
            }else{
                toJson.setMsg("err");
                toJson.setFlag(1);
            }

        }catch (Exception e){
            toJson.setMsg("err");
            toJson.setFlag(1);
        }
        return toJson;
    }

    /**
     * 李阳
     * 2018.8.6
     * 根据 staffName 查找学习经历
     * @param staffName
     * @return
     */
    public ToJson<LearningExperienceWithBLOBs> getByStaffName(String staffName, Integer page, Integer pageSize, boolean useFlag){
        ToJson<LearningExperienceWithBLOBs> json=new ToJson<LearningExperienceWithBLOBs>();
        //分页
        PageParams pageParams = new PageParams();
        pageParams.setPage(page);
        pageParams.setPageSize(pageSize);
        pageParams.setUseFlag(useFlag);
        HashMap<String,Object> map = new HashMap();
        map.put("pageParams", pageParams);
        map.put("staffName",staffName);
        try{
            List<LearningExperienceWithBLOBs> list=learningExperienceMapper.getByStaffName(map);
            if(list.size()>0){
                for(LearningExperienceWithBLOBs learn:list){
                    Users user = usersMapper.findUsersByuserId(learn.getUserId()); //根据 id 找名字
                    if(user!=null){
                        learn.setUserId(user.getUserName());
                    }

                    //学历
                    HrCode hr_staff_contract2 = hrCodeMapper.selectCode("STAFF_HIGHEST_SCHOOL",learn.getAcademic());
                    if (hr_staff_contract2 != null) {
                        learn.setAcademic(hr_staff_contract2.getCodeName());
                    } else {
                        learn.setAcademic(" ");
                    }
                    Users user2 = usersMapper.findUsersByuserId(learn.getCertifier()); //根据 id 找名字
                    if(user!=null){
                        learn.setCertificate(user2.getUserName());
                    }
                }
                json.setObj(list);
            }
            json.setTotleNum(learningExperienceMapper.getByStaffNameCount(staffName));
            json.setFlag(0);
            json.setMsg("true");
        }catch (Exception e){
            json.setMsg("err");
            json.setFlag(1);
        }
        return json;
    }
}
